package com.ruoyi.common.utils.ehcache;

import java.util.List;
import java.util.Objects;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
import net.sf.ehcache.Element;

/**
 * <h5>描述:缓存管理类</h5>
 * @author ...
 */
public class EhcacheUtils {

    public static final String CACHE_NAME = "eqp_state";
    private static final String EHCACHE_CONFIG_NAME = "ehcache.xml";
    private static final CacheManager CACHE_MANAGER;

    static {
        String classesPath = getClassesPath();
        CACHE_MANAGER = CacheManager.create(classesPath + EHCACHE_CONFIG_NAME);
    }

    /**
     * @param cacheName 配置文件中缓存的name
     * @param key        键
     * @param value      缓存值
     * @功能描述： 添加一个缓存到指定的缓存中
     */
    public synchronized static void putObjectCache(String cacheName, String key, Object value) {
        Cache cache = getCache(cacheName);
        if (null != cache) {
            cache.remove(key);
            Element elem = new Element(key, value);
            cache.put(elem);
        }
    }

    /**
     * @param cacheName 配置文件中缓存的name
     * @param key        键
     * @return Object
     * @功能描述： 从指定的缓存中获取一个缓存数据的值
     */
    public static Object getObjectCache(String cacheName, String key) {
        Cache cache = getCache(cacheName);
        if (null != cache) {
            Element element = cache.get(key);
            // isExpired(Element element) 检查缓存是否过期
            if (element != null && !cache.isExpired(element)) {
                return element.getObjectValue();
            }
        }
        return null;
    }

    /**
     * <h5>功能:清空指定缓存的所有信息</h5>
     *
     * @param cacheName 配置文件中缓存的name
     */
    public static void removeCache(String cacheName) {
        Cache cache = getCache(cacheName);
        cache.removeAll();
    }

    /**
     * <h5>功能:清空指定缓存的指定信息</h5>
     *
     * @param cacheName 配置文件中缓存的name
     * @param key        键
     * @return boolean 是否移除成功
     */
    public static boolean removeCache(String cacheName, String key) {
        Cache cache = getCache(cacheName);
        return cache.remove(key);
    }

    /**
     * 停止缓存管理器
     */
    public static void shutdown() {
        if (null != CACHE_MANAGER) {
            CACHE_MANAGER.shutdown();
        }
    }

// ==================== private method ====================

    /**
     * @return classes绝对路径
     * @功能描述： 获取 classes 文件夹路径
     */
    private static String getClassesPath() {
        return Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource("")).getPath();
    }

    /**
     * @param cacheName 配置文件中缓存的name
     * @return Cache 缓存
     * @功能描述： 获取指定名称的缓存
     */
    private static Cache getCache(String cacheName) {
        return CACHE_MANAGER.getCache(cacheName);
    }

    public static void main(String[] args) {
        //user.dir指定了当前的路径
        System.out.println(Thread.currentThread().getContextClassLoader().getResource("").getPath().substring(1));
        EhcacheUtils.putObjectCache(CACHE_NAME, "key01", 1000);
        EhcacheUtils.putObjectCache(CACHE_NAME, "key02", "abc");
        EhcacheUtils.putObjectCache(CACHE_NAME, "key03", 3.1415);
        EhcacheUtils.putObjectCache(CACHE_NAME, "key04", "哈哈");
        EhcacheUtils.putObjectCache(CACHE_NAME, "key05", "{\"id\":100}");

        EhcacheUtils.putObjectCache(CACHE_NAME, "key04", "Hello World");

//      removeCache(CACHE_NAME);
//      removeCache(CACHE_NAME, "key05");
        Cache demoCache = CACHE_MANAGER.getCache(CACHE_NAME);
        if (null != demoCache) {
            List<?> demoList = demoCache.getKeys();
            for (Object myKey : demoList) {
                Element myElement = demoCache.get(myKey);
                System.out.println(myKey + "|" + myElement.getObjectValue());
            }
        }
        EhcacheUtils.putObjectCache(CACHE_NAME, "key03", 3.1666);
        System.out.println("---------" + EhcacheUtils.getObjectCache(CACHE_NAME, "key03"));

        System.exit(0);
    }
}